package com.app.freshmenapp.service.impl;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.freshmenapp.dao.UserDao;
import com.app.freshmenapp.entity.User;
import com.app.freshmenapp.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{
	@Autowired
	UserDao userDao;

	@Override
	public boolean addUser(User user) {
		userDao.save(user);
		if(user.getId()>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean deleteUser(Integer id) {
		if(userDao.existsById(id)) {
			userDao.deleteById(id);
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean updateUser(User user) {
		userDao.save(user);
		if(user.getId()>0) {
			return true;
		}else {
			return false;
		}
		
	}

	@Override
	public User findUser(Integer id) {
		
		Optional<User> opt=  userDao.findById(id);
		return opt.get();
		
	}

	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		return userDao.findAll();
	}

	@Override
	public String validate(String name, String password) {
		List<User> list = findAllUser();
		for(int i=0;i<list.size();i++) {
			if(list.get(i).getName().equals(name) 
					&& list.get(i).getPassword().equals(password)) {
				return list.get(i).getRole();
			}
		}
		return null;
	}

}
